<template>
  <view>
    <!-- 轮播图 -->
    <swiper class="swiper-item" autoplay :interval="3000" :duration="1000" circular>
      <swiper-item class="swiper-list" v-for="(item, i) in swiperList" :key="i">
        <navigator class="swiper-image" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" @click="navClick(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层分类 -->
    <view class="floor-list">
      <!-- 楼层标题 -->
      <view class="floor-title" v-for="(item, i) in floorList" :key="i">
        <image class="title-img" :src="item.floor_title.image_src"></image>
        <!-- 底部图片 -->
        <view class="floor-item">
          <!-- 左侧大图 -->
          <navigator :url="item.product_list[0].url" class="left-floor-item">
            <image mode="widthFix" :style="{width: item.product_list[0].image_width + 'rpx'}" :src="item.product_list[0].image_src"></image>
          </navigator>
          <!-- 右侧小图 -->
          <view class="right-floor-item">
            <navigator :url="item2.url" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0">
              <image mode="widthFix" :style="{width: item2.image_width + 'rpx'}" :src="item2.image_src"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 轮播图数据列表
        swiperList: [],
        // 分类导航数据
        navList: [],
        // 楼层数据列表
        floorList: []
      }
    },
    onLoad() {
      // 页面加载的时候触发
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据请求
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/home/swiperdata')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.swiperList = res.message
      },
      // 获取分类导航栏数据请求
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/home/catitems')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.navList = res.message
      },
      // 当点击分类导航时
      navClick(item) {
        if (item.name === '分类') {
          // 编程式跳转
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据请求
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/home/floordata')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // 通过双层foreach循环，处理url地址
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      }
    }
  }
</script>

<style lang="scss">
  .swiper-item {
    height: 330rpx;

    .swiper-list {

      .swiper-image,
      image {
        width: 100%;
        height: 100%;
      }
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    .nav-item,
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-list {
    .title-img {
      height: 60rpx;
      width: 100%;
      // display: flex;
    }

    .floor-item {
      display: flex;
      padding-left: 10rpx;
    }

    .right-floor-item {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
  }
</style>
